import mixin from '@/service/mixin'
import Pagination from '@/components/pagination'
import UploadImageButton from '@/components/upload-image-button'

export default {
    name: 'operation-co-loan-list',
    components: {
      Pagination,
      UploadImageButton
    },
    mixins: [mixin],
    data () {
      return {
        tableData: [],
        dialogVisible: false,
        modifyData: []
      }
    },
    methods: {
      handleKeyUp (key, index, value) {
        this.modifyData[index][key] = Number(value.replace(/\D+/, ''))
      },
      handleModify () {
        this.modifyData = this.tableData.map(item => {
          // 新户 = 1
          // >= 2
          // normal = 3
          let type = null
          if (item.name === 'NEVER_LOAN_SUCCESS') type = 1
          else if (!item.endDays) type = 2
          else type = 3
          return {
            ...item,
            type,
            error: false
          }
        })
        this.dialogVisible = true
      },
      handleClose () {
        this.dialogVisible = false
      },
      async getPageList () {
        try {
        const { status, data } = await this.$api.operation.activeList()
        if (status === '1') {
          this.tableData = [data[data.length - 1], ...data.slice(0, data.length - 1)] || []
          this.tableMaxHeight = document.body.clientHeight - 220;
        } else {
          this.tableData = []
        }
        } catch (e) {
          console.log(e)
        }
      },
      getDateValue (row) {
        const { name, startDays, endDays } = row || {}
        if (name === 'NEVER_LOAN_SUCCESS') return this.$t('common.newCustomer')
        else {
          if (!endDays) return `>= ${startDays}`
          else return `${startDays} - ${endDays}`
        }
      },
      handleDelete (index) {
        this.modifyData.splice(index, 1)
      },
      handleClick () {
        const data = {
          startDays: '',
          endDays: '',
          maxCount: '',
          newApplyCount: '',
          type: 3,
          startDaysLable: '',
          error: false,
          name: new Date().getTime()
        }
        this.modifyData.splice(this.modifyData.length - 1, 0, data)
      },
      async handleSureModify () { // 确认修改
        for (let i = 0; i < this.modifyData.length -1; i++) {
          if (this.modifyData[i].type === 1) {
            if (!this.modifyData[i].maxCount || !this.modifyData[i].newApplyCount) this.modifyData[i].error = true
            else this.modifyData[i].error = false
          } else {
            const end = Number(this.modifyData[i].endDays)
            const start = Number(this.modifyData[i + 1].startDays)
            const curStart = Number(this.modifyData[i].startDays)
            if (end + 1 !== start || !this.modifyData[i].maxCount || !this.modifyData[i].newApplyCount || curStart > end) this.modifyData[i].error = true
            else this.modifyData[i].error = false
          }
        }
        const obj = this.modifyData[this.modifyData.length - 1]
        this.modifyData[this.modifyData.length - 1].error = !obj.maxCount || !obj.newApplyCount
        const index = this.modifyData.findIndex(item => item.error)
        if (index > -1) {
          this.$message({message: this.$tc('common.modifyFail'), type: 'warning'})
          return
        }
        const params = this.modifyData.map(item => ({
          startDays: item.startDays === null ? null : Number(item.startDays),
          endDays: Number(item.endDays) || null,
          maxCount: Number(item.maxCount),
          newApplyCount: Number(item.newApplyCount)
        }))
        const { status } = await this.$api.operation.addActiveList(params)
        if (status === '1') {
          this.$message({ message: this.$tc('common.modifySuccess'), type: 'warning '})
          this.getPageList()
          this.handleClose()
        }
      }
    },
    created () {
      this.getPageList()
    }
  }